package com.goodfox.web.generic.attribute.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goodfox.web.generic.attribute.entity.AtrType;
import com.goodfox.web.generic.attribute.entity.Attribute;
import com.goodfox.web.generic.attribute.mapper.AttributeMapper;
import com.goodfox.web.generic.attribute.service.AttributeService;
import com.goodfox.web.generic.provide.annotation.NoteExplain;
import com.goodfox.web.generic.provide.entity.Pager;
import com.goodfox.web.generic.provide.util.AttributeUtil;
import com.goodfox.web.generic.provide.util.ParameterUtil;

/**
 * 属性Service实现类
 * @author qin gt
 *
 */
@Transactional
@Service(value="attributeService")
@NoteExplain(comment="属性管理")
public class AttributeServiceImpl implements AttributeService {

	/** log4j日志器 */
	private Logger logger = Logger.getLogger(AttributeServiceImpl.class);
	
	/** 属性Mapper */
	@Autowired
	private AttributeMapper attributeMapper;
	
	@NoteExplain(comment="查询属性类型列表")
	public Pager<AtrType> queryAtrTypePager(int currentPage,int numPerPage,Map map){
		int start=(currentPage-1)*numPerPage;
		Long allCount=attributeMapper.selectAtrTypeListCount(map);
		List<AtrType> list=attributeMapper.selectAtrTypeList(map,new RowBounds(start,numPerPage));
		for(AtrType atrType:list){
			fillingAtrType(atrType);
		}
		return new Pager<AtrType>(allCount,list);
	}

	@NoteExplain(comment="查询属性类型列表")
	public List<AtrType> queryAtrTypeList(){
		return attributeMapper.selectAtrTypeList(new HashMap());
	}

	@NoteExplain(comment="查询属性类型列表")
	public AtrType getAtrTypeByValue(String typeValue){
		AtrType atrType=attributeMapper.selectAtrTypeByValue(typeValue);
		fillingAtrType(atrType);
		return atrType;
	}

	@NoteExplain(comment="查询属性内容列表")
	public List<Attribute> queryAttributeListByTypeId(String typeValue){
		return attributeMapper.selectAttributeListByTypeId(typeValue);
	}

	@NoteExplain(comment="查询属性内容")
	public Attribute getAttribute(String atrId){
		return attributeMapper.selectAttributeById(atrId);
	}

	@NoteExplain(comment="新增属性内容",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addAttribute(Attribute entity,String loginLogId){
		Long count=attributeMapper.selectAttributeCountByType(entity.getAtrTValue());
		entity.setAtrValue(String.valueOf(count+1));
		entity.setAtrCode(entity.getAtrTValue()+ParameterUtil.append0ForNum(entity.getAtrValue(), 2, true));
		attributeMapper.insertAttribute(entity);
		reloadAttributeList();
	}
	
	@NoteExplain(comment="更新属性内容",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyAttribute(AtrType oldEny,AtrType newEny,String loginLogId){
		if(newEny.getAtrIdList()!=null && newEny.getAtrIdList().size()==newEny.getAtrSortList().size() && newEny.getAtrIdList().size()==newEny.getAtrNameList().size()){
			StringBuilder sbAtr=new StringBuilder();
			for(int i=0;i<newEny.getAtrIdList().size();i++){
				if(newEny.getAtrIdList().get(i)!=null){
					Attribute atr=new Attribute();
					atr.setAtrId(newEny.getAtrIdList().get(i).toString());
					if(newEny.getAtrSortList().get(i)!=null){
						atr.setAtrSort(Long.parseLong(newEny.getAtrSortList().get(i).toString()));
					}
					if(newEny.getAtrNameList().get(i)!=null){
						atr.setAtrName(newEny.getAtrNameList().get(i).toString());
					}
					attributeMapper.updateAttribute(atr);
					sbAtr.append("[").append(atr.getAtrSort()).append(":").append(atr.getAtrName()).append("] ");
				}
			}
			newEny.setAtrNames(sbAtr.toString());
			reloadAttributeList();
		}
	}
	
	/**
	 * 查询全部属性列表
	 * @return
	 */
	public List<Attribute> queryAttributeList(){
		return attributeMapper.selectAttributeList(new HashMap());
	}
	
	private void fillingAtrType(AtrType atrType){
		StringBuilder sbAtr=new StringBuilder();
		for(Attribute atr:queryAttributeListByTypeId(atrType.getTypeValue())){
			sbAtr.append("[").append(atr.getAtrSort()).append(":").append(atr.getAtrName()).append("] ");
		}
		atrType.setAtrNames(sbAtr.toString());
	}
	
	/**
	 * 初始化属性列表
	 */
	public void reloadAttributeList(){
		AttributeUtil.ATTR_LIST=queryAttributeList();
	}
}
